import type { Classify } from '@/api/types/classify'

export const filterChild = (name?: string, status?: string, node?: Classify) => {
  if (node) {
    node.children = node?.children?.map(node => filterChild(name, status, node)).filter(node => node)
    if ((filterName(node, name) && filterStatus(node, status)) || (node.children && node.children.length > 0)) {
      return node
    }
  }
}

/**
 * 名字过滤
 */
const filterName = (row: Classify, name?: string) => {
  if (name === undefined || name === "") {
    return true
  }
  return row.name?.toLowerCase().includes(name)
}

/**
 * 状态过滤
 */
const filterStatus = (row: Classify, status?: string) => {
  if (status === undefined || status === "") {
    return true
  }
  return row.status === status
}

export const deepCopy = (classify: Classify) => {
  let node = { ...classify }
  node.children = node.children?.map(node => {
    if (node) return deepCopy(node)
  }).filter(node => node)
  return node
}